﻿/*
 * Copyright 2012 Nascent Digital (http://www.nascentdigital.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0 
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
using System;


namespace Nascent.GoogleAnalytics
{
    /// <summary>
    /// Provides a set of static and extension methods supporting the analytics
    /// library internals.
    /// </summary>
    internal static class AnalyticsHelper
    {
        #region class variables

        private static readonly DateTime _epoch;

        #endregion


        #region constructors

        static AnalyticsHelper()
        {
            // initialize class variables
            _epoch = new DateTime(1970, 1, 1, 0, 0, 0, 0);
        }

        #endregion


        #region extension methods

        public static long ToUnixTimestamp(this DateTime time)
        {
            double elapsedTime = (time - _epoch).TotalSeconds;
            return Convert.ToInt64(elapsedTime);
        }

        public static DateTime ToDateTime(this long timestamp)
        {
            DateTime time = _epoch.AddSeconds(timestamp);
            return time;
        }

        #endregion

    }  // class AnalyticsHelper

}  // namespace Nascent.GoogleAnalytics
